package com.huawei.bitfun.insight.utils;

import com.intellij.openapi.application.PathManager;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

public class Common {
    private static final String FORWARD_SLASH_STR;

    public static final char FORWARD_SLASH = 47;

    public static final String DOMAIN_NAME = "localhost";

    public static final String URL_PREFIX = "http://";

    public static final String FRONTEND_URL = URL_PREFIX + DOMAIN_NAME + "/index.html";

    public static final String FRONTEND_DIR = "frontend";

    public static final String HIPERF_SOURCE_DATA_FILE_EXTENSION = "data";

    public static final String ARK_CPU_SOURCE_DATA_FILE_EXTENSION = "cpuprofile";

    public static final String MANIFEST_FILE_NAME = "manifest.json";

    static {
        char[] forwardSlashChars = {FORWARD_SLASH};
        FORWARD_SLASH_STR = new String(forwardSlashChars);
    }

    public static String joinWithForwardSlash(String... strArr) {
        return String.join(FORWARD_SLASH_STR, strArr);
    }

    public static String getCacheRootPath() {
        String configPath = PathManager.getConfigPath();
        return joinWithForwardSlash(configPath.replace('\\', '/'), "profilerEx");
    }

    public static String getCacheDBPath() {
        return joinWithForwardSlash(getCacheRootPath(), "db");
    }

    public static String getDbFilePath(String dbFileName) {
        String cacheDbPath = getCacheDBPath();
        return joinWithForwardSlash(cacheDbPath, dbFileName);
    }

    public static String getInsightExDbFilePath() {
        return joinWithForwardSlash(getCacheDBPath(), "profilerExSql.db");
    }

    public static String getDestPath(String fileBaseName) {
        return joinWithForwardSlash(getCacheRootPath(), fileBaseName);
    }
//
//    public static String getTmpPath() {
//        return joinWithForwardSlash(getCacheRootPath(), "tmp", Common.getLocalTime());
//    }

    public static @NotNull String getLocalTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd HHmmss")).replace(" ", "T");
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}
